<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>colormenubutton.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_colormenubutton.js.html">colormenubutton.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A color menu button.  Extends {@link goog.ui.MenuButton} by
<a name="line17"></a> * showing the currently selected color in the button caption.
<a name="line18"></a> *
<a name="line19"></a> * @author robbyw@google.com (Robby Walker)
<a name="line20"></a> * @author attila@google.com (Attila Bodis)
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.ui.ColorMenuButton&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.object&#39;);
<a name="line27"></a>goog.require(&#39;goog.ui.ColorMenuButtonRenderer&#39;);
<a name="line28"></a>goog.require(&#39;goog.ui.ColorPalette&#39;);
<a name="line29"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.ControlContent&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.Menu&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.MenuButton&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.registry&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * A color menu button control.  Extends {@link goog.ui.MenuButton} by adding
<a name="line39"></a> * an API for getting and setting the currently selected color from a menu of
<a name="line40"></a> * color palettes.
<a name="line41"></a> *
<a name="line42"></a> * @param {goog.ui.ControlContent} content Text caption or existing DOM
<a name="line43"></a> *     structure to display as the button&#39;s caption.
<a name="line44"></a> * @param {goog.ui.Menu=} opt_menu Menu to render under the button when clicked;
<a name="line45"></a> *     should contain at least one {@link goog.ui.ColorPalette} if present.
<a name="line46"></a> * @param {goog.ui.MenuButtonRenderer=} opt_renderer Button renderer;
<a name="line47"></a> *     defaults to {@link goog.ui.ColorMenuButtonRenderer}.
<a name="line48"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM hepler, used for
<a name="line49"></a> *     document interaction.
<a name="line50"></a> * @constructor
<a name="line51"></a> * @extends {goog.ui.MenuButton}
<a name="line52"></a> */
<a name="line53"></a>goog.ui.ColorMenuButton = function(content, opt_menu, opt_renderer,
<a name="line54"></a>    opt_domHelper) {
<a name="line55"></a>  goog.ui.MenuButton.call(this, content, opt_menu, opt_renderer ||
<a name="line56"></a>      goog.ui.ColorMenuButtonRenderer.getInstance(), opt_domHelper);
<a name="line57"></a>};
<a name="line58"></a>goog.inherits(goog.ui.ColorMenuButton, goog.ui.MenuButton);
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>/**
<a name="line62"></a> * Default color palettes.
<a name="line63"></a> * @type {!Object}
<a name="line64"></a> */
<a name="line65"></a>goog.ui.ColorMenuButton.PALETTES = {
<a name="line66"></a>  /** Default grayscale colors. */
<a name="line67"></a>  GRAYSCALE: [
<a name="line68"></a>    &#39;#000&#39;, &#39;#444&#39;, &#39;#666&#39;, &#39;#999&#39;, &#39;#ccc&#39;, &#39;#eee&#39;, &#39;#f3f3f3&#39;, &#39;#fff&#39;
<a name="line69"></a>  ],
<a name="line70"></a>
<a name="line71"></a>  /** Default solid colors. */
<a name="line72"></a>  SOLID: [
<a name="line73"></a>    &#39;#f00&#39;, &#39;#f90&#39;, &#39;#ff0&#39;, &#39;#0f0&#39;, &#39;#0ff&#39;, &#39;#00f&#39;, &#39;#90f&#39;, &#39;#f0f&#39;
<a name="line74"></a>  ],
<a name="line75"></a>
<a name="line76"></a>  /** Default pastel colors. */
<a name="line77"></a>  PASTEL: [
<a name="line78"></a>    &#39;#f4cccc&#39;, &#39;#fce5cd&#39;, &#39;#fff2cc&#39;, &#39;#d9ead3&#39;, &#39;#d0e0e3&#39;, &#39;#cfe2f3&#39;, &#39;#d9d2e9&#39;,
<a name="line79"></a>      &#39;#ead1dc&#39;,
<a name="line80"></a>    &#39;#ea9999&#39;, &#39;#f9cb9c&#39;, &#39;#ffe599&#39;, &#39;#b6d7a8&#39;, &#39;#a2c4c9&#39;, &#39;#9fc5e8&#39;, &#39;#b4a7d6&#39;,
<a name="line81"></a>      &#39;#d5a6bd&#39;,
<a name="line82"></a>    &#39;#e06666&#39;, &#39;#f6b26b&#39;, &#39;#ffd966&#39;, &#39;#93c47d&#39;, &#39;#76a5af&#39;, &#39;#6fa8dc&#39;, &#39;#8e7cc3&#39;,
<a name="line83"></a>      &#39;#c27ba0&#39;,
<a name="line84"></a>    &#39;#cc0000&#39;, &#39;#e69138&#39;, &#39;#f1c232&#39;, &#39;#6aa84f&#39;, &#39;#45818e&#39;, &#39;#3d85c6&#39;, &#39;#674ea7&#39;,
<a name="line85"></a>      &#39;#a64d79&#39;,
<a name="line86"></a>    &#39;#990000&#39;, &#39;#b45f06&#39;, &#39;#bf9000&#39;, &#39;#38761d&#39;, &#39;#134f5c&#39;, &#39;#0b5394&#39;, &#39;#351c75&#39;,
<a name="line87"></a>      &#39;#741b47&#39;,
<a name="line88"></a>    &#39;#660000&#39;, &#39;#783f04&#39;, &#39;#7f6000&#39;, &#39;#274e13&#39;, &#39;#0c343d&#39;, &#39;#073763&#39;, &#39;#20124d&#39;,
<a name="line89"></a>      &#39;#4c1130&#39;
<a name="line90"></a>  ]
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Value for the &quot;no color&quot; menu item object in the color menu (if present).
<a name="line96"></a> * The {@link goog.ui.ColorMenuButton#handleMenuAction} method interprets
<a name="line97"></a> * ACTION events dispatched by an item with this value as meaning &quot;clear the
<a name="line98"></a> * selected color.&quot;
<a name="line99"></a> * @type {string}
<a name="line100"></a> */
<a name="line101"></a>goog.ui.ColorMenuButton.NO_COLOR = &#39;none&#39;;
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Factory method that creates and returns a new {@link goog.ui.Menu} instance
<a name="line106"></a> * containing default color palettes.
<a name="line107"></a> * @param {Array.&lt;goog.ui.Control&gt;=} opt_extraItems Optional extra menu items to
<a name="line108"></a> *     add before the color palettes.
<a name="line109"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM hepler, used for
<a name="line110"></a> *     document interaction.
<a name="line111"></a> * @return {goog.ui.Menu} Color menu.
<a name="line112"></a> */
<a name="line113"></a>goog.ui.ColorMenuButton.newColorMenu = function(opt_extraItems, opt_domHelper) {
<a name="line114"></a>  var menu = new goog.ui.Menu(opt_domHelper);
<a name="line115"></a>
<a name="line116"></a>  if (opt_extraItems) {
<a name="line117"></a>    goog.array.forEach(opt_extraItems, function(item) {
<a name="line118"></a>      menu.addChild(item, true);
<a name="line119"></a>    });
<a name="line120"></a>  }
<a name="line121"></a>
<a name="line122"></a>  goog.object.forEach(goog.ui.ColorMenuButton.PALETTES, function(colors) {
<a name="line123"></a>    var palette = new goog.ui.ColorPalette(colors, null, opt_domHelper);
<a name="line124"></a>    palette.setSize(8);
<a name="line125"></a>    menu.addChild(palette, true);
<a name="line126"></a>  });
<a name="line127"></a>
<a name="line128"></a>  return menu;
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Returns the currently selected color (null if none).
<a name="line134"></a> * @return {?string} The selected color.
<a name="line135"></a> */
<a name="line136"></a>goog.ui.ColorMenuButton.prototype.getSelectedColor = function() {
<a name="line137"></a>  return /** @type {string} */ (this.getValue());
<a name="line138"></a>};
<a name="line139"></a>
<a name="line140"></a>
<a name="line141"></a>/**
<a name="line142"></a> * Sets the selected color, or clears the selected color if the argument is
<a name="line143"></a> * null or not any of the available color choices.
<a name="line144"></a> * @param {?string} color New color.
<a name="line145"></a> */
<a name="line146"></a>goog.ui.ColorMenuButton.prototype.setSelectedColor = function(color) {
<a name="line147"></a>  this.setValue(color);
<a name="line148"></a>};
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Sets the value associated with the color menu button.  Overrides
<a name="line153"></a> * {@link goog.ui.Button#setValue} by interpreting the value as a color
<a name="line154"></a> * spec string.
<a name="line155"></a> * @param {*} value New button value; should be a color spec string.
<a name="line156"></a> * @override
<a name="line157"></a> */
<a name="line158"></a>goog.ui.ColorMenuButton.prototype.setValue = function(value) {
<a name="line159"></a>  var color = /** @type {?string} */ (value);
<a name="line160"></a>  for (var i = 0, item; item = this.getItemAt(i); i++) {
<a name="line161"></a>    if (typeof item.setSelectedColor == &#39;function&#39;) {
<a name="line162"></a>      // This menu item looks like a color palette.
<a name="line163"></a>      item.setSelectedColor(color);
<a name="line164"></a>    }
<a name="line165"></a>  }
<a name="line166"></a>  goog.ui.ColorMenuButton.superClass_.setValue.call(this, color);
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Handles {@link goog.ui.Component.EventType.ACTION} events dispatched by
<a name="line172"></a> * the menu item clicked by the user.  Updates the button, calls the superclass
<a name="line173"></a> * implementation to hide the menu, stops the propagation of the event, and
<a name="line174"></a> * dispatches an ACTION event on behalf of the button itself.  Overrides
<a name="line175"></a> * {@link goog.ui.MenuButton#handleMenuAction}.
<a name="line176"></a> * @param {goog.events.Event} e Action event to handle.
<a name="line177"></a> * @override
<a name="line178"></a> */
<a name="line179"></a>goog.ui.ColorMenuButton.prototype.handleMenuAction = function(e) {
<a name="line180"></a>  if (typeof e.target.getSelectedColor == &#39;function&#39;) {
<a name="line181"></a>    // User clicked something that looks like a color palette.
<a name="line182"></a>    this.setValue(e.target.getSelectedColor());
<a name="line183"></a>  } else if (e.target.getValue() == goog.ui.ColorMenuButton.NO_COLOR) {
<a name="line184"></a>    // User clicked the special &quot;no color&quot; menu item.
<a name="line185"></a>    this.setValue(null);
<a name="line186"></a>  }
<a name="line187"></a>  goog.ui.ColorMenuButton.superClass_.handleMenuAction.call(this, e);
<a name="line188"></a>  e.stopPropagation();
<a name="line189"></a>  this.dispatchEvent(goog.ui.Component.EventType.ACTION);
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * Opens or closes the menu.  Overrides {@link goog.ui.MenuButton#setOpen} by
<a name="line195"></a> * generating a default color menu on the fly if needed.
<a name="line196"></a> * @param {boolean} open Whether to open or close the menu.
<a name="line197"></a> * @param {goog.events.Event=} opt_e Mousedown event that caused the menu to
<a name="line198"></a> *     be opened.
<a name="line199"></a> * @override
<a name="line200"></a> */
<a name="line201"></a>goog.ui.ColorMenuButton.prototype.setOpen = function(open, opt_e) {
<a name="line202"></a>  if (open &amp;&amp; this.getItemCount() == 0) {
<a name="line203"></a>    this.setMenu(
<a name="line204"></a>        goog.ui.ColorMenuButton.newColorMenu(null, this.getDomHelper()));
<a name="line205"></a>    this.setValue(/** @type {?string} */ (this.getValue()));
<a name="line206"></a>  }
<a name="line207"></a>  goog.ui.ColorMenuButton.superClass_.setOpen.call(this, open, opt_e);
<a name="line208"></a>};
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>// Register a decorator factory function for goog.ui.ColorMenuButtons.
<a name="line212"></a>goog.ui.registry.setDecoratorByClassName(
<a name="line213"></a>    goog.ui.ColorMenuButtonRenderer.CSS_CLASS,
<a name="line214"></a>    function() {
<a name="line215"></a>      return new goog.ui.ColorMenuButton(null);
<a name="line216"></a>    });
</pre>


</body>
</html>
